package com.keyinong.qudan.dapter;

import java.util.ArrayList;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.keyinong.bluetooth.BluetoothActivity;
import com.keyinong.qudan.R;
import com.keyinong.qudan.ResultActivity;
import com.keyinong.qudan.ScanActivity;
import com.keyinong.qudan.entity.OrderEntity;
import com.keyinong.qudan.tools.HttpTools;
import com.keyinong.qudan.view.OrderDetailActivity;
import com.keyinong.wifiprint.WifiPrintActivity;
import com.loopj.android.http.JsonHttpResponseHandler;

public class OrderAdapter extends BaseAdapter{
	Context context;
	ArrayList<OrderEntity> list;
	HttpTools httpTools=null;
	
	public OrderAdapter(Context context,
		ArrayList<OrderEntity> list) {
		super();
		this.context = context;
		this.list = list;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@SuppressLint("ResourceAsColor")
	@Override
	public View getView(final int position, View view, ViewGroup parent) {
		final Handler handler;
		if (view == null) {
			handler = new Handler();
			view = LayoutInflater.from(context).inflate(
					R.layout.item_order, null);
			handler.tv_id=(TextView)view.findViewById(R.id.tv_id);
			handler.tv_time=(TextView)view.findViewById(R.id.tv_time);
			handler.tv_user=(TextView)view.findViewById(R.id.tv_user);
			handler.tv_address=(TextView)view.findViewById(R.id.tv_address);
			handler.tv_phone=(TextView)view.findViewById(R.id.tv_phone);
			handler.tv_state=(TextView)view.findViewById(R.id.tv_state);
			handler.btn_queren=(Button)view.findViewById(R.id.btn_queren);
			handler.btn_dayin=(Button)view.findViewById(R.id.btn_dayin);
			handler.btn_xiangqing=(Button)view.findViewById(R.id.btn_xiangqiang);
			handler.tv_qucanfangshi=(TextView)view.findViewById(R.id.tv_qucanfangshi);
			view.setTag(handler);
		} else {
			handler = (Handler) view.getTag();
		}
		handler.tv_id.setText(list.get(position).getOrderno());
		handler.tv_time.setText(list.get(position).getReceive_time());
		handler.tv_phone.setText(list.get(position).getPhone());
		handler.tv_user.setText(list.get(position).getName());
		if(list.get(position).getState().equals("1")){
			handler.tv_state.setText("待确认");
			handler.btn_queren.setText("接单");
			handler.btn_queren.setEnabled(true);
		}else if(list.get(position).getState().equals("2")){
			handler.tv_state.setText("待取餐");
			
			if (list.get(position).getRecmode().equals("2")){
				handler.btn_queren.setText("发送给达达");
			}else {
				handler.btn_queren.setText("已接单");
			}
//			handler.btn_queren.setBackgroundResource(R.drawable.red);
			handler.btn_queren.setEnabled(false);
		}else if(list.get(position).getState().equals("3")){
			handler.tv_state.setText("已完成");
			handler.btn_queren.setText("已完成");
			handler.btn_queren.setEnabled(false);
		}
		
		if (list.get(position).getAddress().equals("null")||list.get(position).getAddress().equals("")) {
			handler.tv_address.setText("无");
		}else {
			handler.tv_address.setText(list.get(position).getAddress());
		}
		if(list.get(position).getRecmode().equals("1")){
			handler.tv_qucanfangshi.setText("店内自提");
		}else if (list.get(position).getRecmode().equals("2")){
			handler.tv_qucanfangshi.setText("送货上门");
		}else if (list.get(position).getRecmode().equals("3")){
		handler.tv_qucanfangshi.setText("堂吃");
	}
//		handler.btn_dayin.setOnClickListener(new OnClickListener() {
//			
//			@Override
//			public void onClick(View v) {
//				Intent intent = new Intent();
//				intent.putExtra("address", list.get(position).getAddress());
//				intent.setClass(context, WifiPrintActivity.class);
//				context.startActivity(intent);
//			}
//		});
		handler.btn_xiangqing.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setClass(context, BluetoothActivity.class);
				intent.putExtra("id", list.get(position).getId());
//				intent.putExtra("state", list.get(position).getState());
				intent.putExtra("address", list.get(position).getAddress());
				context.startActivity(intent);
			}
		});
		handler.btn_dayin.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setClass(context, WifiPrintActivity.class);
				intent.putExtra("address", list.get(position).getAddress());
				intent.putExtra("id", list.get(position).getId());
				context.startActivity(intent);
			}
		});
		
		handler.btn_queren.setOnClickListener(new OnClickListener() {
			SharedPreferences sp = context.getSharedPreferences("spReg",
					Context.MODE_PRIVATE);
			String token = sp.getString("token", "");
			@Override
			public void onClick(View v) {
				if (httpTools == null) {
					httpTools = new HttpTools(context);
				}
				httpTools.yiwancheng(token, list.get(position).getId(),"2",list.get(position).getPaymodeid(),
						new JsonHttpResponseHandler() {

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							JSONObject response) {
						super.onSuccess(statusCode, headers, response);
						try {
							boolean code = response.getBoolean("code");
							String msg = response.getString("msg");
							Log.i("zq", "adapter---"+response.toString());
							
							if (code == true) {
								Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
								//										view.setBackgroundResource(R.drawable.shouchang1);
								handler.btn_queren.setText("已接单");
								list.get(position).setState("2");
								handler.btn_queren.setEnabled(false);
							} else {
								Toast.makeText(context, "已接单", Toast.LENGTH_SHORT).show();
							}
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}

				});

			}
		});
		return view;
	}
	class Handler {
		TextView tv_id;
		TextView tv_time;
		TextView tv_user;
		TextView tv_address;
		TextView tv_phone;
		TextView tv_state;
		TextView tv_qucanfangshi;
		Button btn_queren;
		Button btn_dayin;
		Button btn_xiangqing;
	}
}
